﻿using System.Collections.Generic;
using System.Threading.Tasks;
using NReader.Model.RAS;

namespace NReader.Service
{
    public class ChapterService
    {
        public static async Task SaveMaxChapters(int articleid, IList<ChapterInfo> list)
        {
            await Local.Service.LocalXmlService.SaveMaxChapters(articleid, list);
        }

        public static async Task<IList<ChapterInfo>> GetMaxChaptersLocal(int articleid)
        {
            return await Local.Service.LocalXmlService.GetMaxChapters(articleid);
        }

        public static async Task SaveBookReader(int articleid, string webflag, IList<ChapterInfo> list)
        {
            await Local.Service.LocalXmlService.SaveBookReader(articleid, webflag, list);
        }

        public static async Task<IList<ChapterInfo>> GetBookReaderLocal(int articleid, string webflag)
        {
            return await Local.Service.LocalXmlService.GetBookReader(articleid, webflag);
        }

        public static async Task<IList<ChapterInfo>> GetBookReader(int articleid, string webflag)
        {
            var client = new RemoteArticleServiceClient();
            var list = await client.GetBookReaderAsync(articleid, webflag);
            return list;
        }

        public static async Task<IList<ChapterInfo>> GetBookReaderByWeb(string webflag, int articleid, int begin,
                                                                        int count)
        {
            var client = new RemoteArticleServiceClient();
            var list = await client.GetChapterListByWebAsync(webflag, articleid, begin, count);
            return list;
        }

        public static async Task<int> GetBookReaderCountByWeb(string webflag, int articleid)
        {
            var client = new RemoteArticleServiceClient();
            var count = await client.GetChapterListCountByWebAsync(webflag, articleid);
            return count;
        }

        public static async Task<string> GetChapterTxt(string webflag, int articleid, int chapterid)
        {
            return await Local.Service.LocalFileService.GetChapterTxt(webflag, articleid, chapterid);
        }

        public static async Task<bool> SaveChapterTxt(string webflag, int articleid, int chapterid, string content)
        {
            return await Local.Service.LocalFileService.SaveChapterTxt(webflag, articleid, chapterid, content);
        }

        /// <summary>
        /// 保存CHAPTER到会话,用于导航(由于导航传入实体类时,挂起时失败，所以只能传入基类)
        /// </summary>
        /// <param name="chapter"></param>
        /// <returns></returns>
        public static async Task SaveChapterSession(ChapterInfo chapter)
        {
            await Local.Service.LocalXmlService.SaveChapter(chapter);
        }

        ///// <summary>
        ///// 从session获取章节信息
        ///// </summary>
        ///// <param name="chapterid"></param>
        ///// <returns></returns>
        //public static async Task<ChapterInfo> GetChapterSession(int chapterid)
        //{
        //    return await Local.Service.LocalXmlService.GetChapter(chapterid);
        //}

        /// <summary>
        /// 从session获取章节信息
        /// </summary>
        /// <param name="chapterid"></param>
        /// <returns></returns>
        public static async Task<ChapterInfo> GetChapterSession(int articleid,int chapterid)
        {
            return await Local.Service.LocalXmlService.GetChapter(articleid,chapterid);
        }
    }
}
